package com.tx.activemq._01_c_p._01_queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * activeMQ生产者-队列
 */
public class Producer {
    public static final String DEFAULT_URL = "tcp://192.168.254.30:61616";
    private static final String QUEUE_NAME = "mercury-test";

    public static void main(String[] args) throws JMSException {

        //1. activeMQConnetionFactory
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(DEFAULT_URL);

        //2. activeMQConnection
        Connection connection = factory.createConnection();

        //3. 开启连接 connection
        connection.start();

        //4. Session
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);


        //5. 设置队列
        Queue queue = session.createQueue(QUEUE_NAME);

        //6.创建生产者 producer
        MessageProducer producer = session.createProducer(queue);


        //7.  创建消息并发送
        for (int i = 1; i <= 6 ; i++) {
            TextMessage msg = session.createTextMessage("msg:" + i);
            producer.send(msg);
        }

        System.out.println("---推送消息完毕---");

        //8.关闭连接
        producer.close();
        session.close();
        connection.close();
    }
}